package com.ia2014.tp.portal2.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ia2014.tp.portal2.facades.FacadeVenta;

@WebServlet("/confirmarCarrito")
public class ConfirmarCarritoServlet extends AbstractServlet{
	private static final long serialVersionUID = 6522585685118979330L;
	
	@EJB
	private FacadeVenta facade;
	
	
	public ConfirmarCarritoServlet(){
		super();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.comprobarSession(req, resp);
		this.process(req, resp);
	}

	
	private void process(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException{
		//Levanto la session y saco los parametros de configuracion
		HttpSession session  = req.getSession(false);
		String redirect = "verCarrito";
		
		this.getMapItemsFromSession(session);
		if (this.itemsVo.size() <= 0){
			redirect = "verCarrito?error=1";
		}
		else{
			this.facade.setItemsVo(this.itemsVo);
			this.facade.setUsuarioVo(this.getUsuarioVOFromSession(session));
			this.facade.confirmarVenta();
			
			this.vaciarMapItemFromSession(session);
			this.facade.setItemsVo(this.itemsVo);	
		}

		resp.sendRedirect(redirect);
	}	
}
